#include "./status_task.h"
#include "../driver/cmd_uart.h"
#include "../driver/sensor.h"
#include "../driver/analog.h"

namespace task {

StatusTask statusTask;
using namespace driver;
using namespace utils;
using namespace conn;

constexpr uint16_t APP_VERSION = 0x09;
static uint8_t statusSeq = 0;

void StatusTask::run() {

    for (;;) {
        notify();
        osDelay(500);
    }
}

uint32_t StatusTask::read_sw() {
    uint32_t v = 0;
    for (uint8_t i = 0; i < 13; i ++) {
        v |= sensor.is_col_checked(i + 1) << i;
    }
    v |= sensor.is_door_close() << 13;
    v |= sensor.is_elock_close() << 14;
    v |= sensor.is_raster() << 15;
    v |= sensor.is_raster2() << 16;
    return v;
}

void StatusTask::notify() {
    uint32_t sw = read_sw();

    cmdUart.notify(statusSeq, proto::CMD_STATUS, 
        APP_VERSION,
        sw,
        analog.a2(),
        analog.a3()
    );
    statusSeq += 1;
}

}
